<?php

namespace App\Repositories;

use App\Models\Cart;

class CartRepo
{
   public function addToCart($user_id,$product)
   {
      
        $data = [
                'user_id' => $user_id,
                'product_name' => $product['product_name'],
                'spec_info'=> empty($product['spec_info']) ? '' : $product['spec_info'],
                'product_id' => $product['product_id'],
                'product_sku_id' => empty($product['product_sku_id']) ? 0 : $product['product_sku_id'],
                'spec_type' => $product['spec_type'],
                'num' => $product['num']
         ];
       
       return Cart::create($data);
   }
  
    public function delFromCart($cart_ids)
    {
        return Cart::where('id','in',$cart_ids)->delete();
    }

    public function userCart($user_id)
    {
        return Cart::where('user_id',$user_id)->with([
            'product'=>function($query){
                $query->select('id','image','name','price','desc','tip');
            },
            'product_sku'=>function($query){
                $query->select('id','image','spec','price');
            }
        ])->get();
    }
    public function del($id)
    {
        return Cart::where('id',$id)->delete();
    }
}